package reservations

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/patients"
	"github.com/flipped-aurora/gin-vue-admin/server/model/reservations"
	reservationsReq "github.com/flipped-aurora/gin-vue-admin/server/model/reservations/request"
	"github.com/gofrs/uuid/v5"
	"gorm.io/gorm"
	"time"
)

type ReservationService struct{}

// CreateReservation 创建reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) CreateReservation(reservation *reservations.Reservation) (err error) {
	v4, _ := uuid.NewV4()
	s := v4.String() + time.Now().Format("20060102150405")
	reservation.ReservationSn = s
	err = global.GVA_DB.Create(reservation).Error
	return err
}

// DeleteReservation 删除reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) DeleteReservation(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&reservations.Reservation{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&reservations.Reservation{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteReservationByIds 批量删除reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) DeleteReservationByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&reservations.Reservation{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&reservations.Reservation{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateReservation 更新reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) UpdateReservation(reservation reservations.Reservation) (err error) {
	err = global.GVA_DB.Model(&reservations.Reservation{}).Where("id = ?", reservation.ID).Updates(&reservation).Error
	return err
}

// GetReservation 根据ID获取reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) GetReservation(ID string) (two ResponseReservation, err error) {
	var reservation reservations.Reservation
	err = global.GVA_DB.Where("id = ?", ID).First(&reservation).Error
	var one patients.Patient
	global.GVA_DB.Where("id=?", reservation.PatientId).Find(&one)
	two = ResponseReservation{
		GVA_MODEL: global.GVA_MODEL{
			ID:        reservation.ID,
			CreatedAt: reservation.CreatedAt,
			UpdatedAt: reservation.UpdatedAt,
			DeletedAt: reservation.DeletedAt,
		},
		ReservationSn:   reservation.ReservationSn,
		PatientId:       reservation.PatientId,
		PatientCardNum:  one.PatientCardNum,
		Name:            one.Name,
		Sex:             one.Sex,
		Mobile:          one.Mobile,
		ReservationObj:  reservation.ReservationObj,
		ReservationType: reservation.ReservationType,
		Department:      reservation.Department,
		PayType:         reservation.PayType,
		Price:           reservation.Price,
		Menu:            reservation.Menu,
		CreatedBy:       reservation.CreatedBy,
		UpdatedBy:       reservation.UpdatedBy,
		DeletedBy:       reservation.DeletedBy,
	}
	return two, nil
}

// reservation表 结构体  Reservation
type ResponseReservation struct {
	global.GVA_MODEL
	ReservationSn   string   `json:"reservationSn" gorm:"column:reservation_sn;comment:预约号;size:100;"`
	PatientId       *int     `json:"patientId" form:"patientId" gorm:"column:patient_id;comment:患者编号;size:10;" binding:"required"`                 //患者名称
	PatientCardNum  *int     `json:"patientCardNum" form:"patientCardNum" gorm:"column:patient_card_num;comment:就诊卡号;size:10;" binding:"required"` //就诊卡号
	Name            *string  `json:"name" form:"name" gorm:"index;column:name;comment:患者姓名;size:30;" binding:"required"`                           //患者姓名
	Sex             *string  `json:"sex" form:"sex" gorm:"column:sex;comment:性别;size:20;"`                                                         //性别
	Mobile          *string  `json:"mobile" form:"mobile" gorm:"uniqueIndex;column:mobile;comment:手机号码;size:20;" binding:"required"`
	ReservationObj  *string  `json:"reservationObj" form:"reservationObj" gorm:"column:reservation_obj;comment:预约项目;size:20;" binding:"required"`     //预约项目
	ReservationType *string  `json:"reservationType" form:"reservationType" gorm:"column:reservation_type;comment:预约号类型;size:20;" binding:"required"` //预约号类型
	Department      *int     `json:"department" form:"department" gorm:"column:department;comment:科室;size:10;"`                                       //科室
	PayType         *string  `json:"payType" form:"payType" gorm:"column:pay_type;comment:支付方式;size:20;" binding:"required"`                          //支付方式
	Price           *float64 `json:"price" form:"price" gorm:"column:price;comment:金额;size:10;"`                                                      //金额
	Menu            *string  `json:"menu" form:"menu" gorm:"column:menu;comment:备注说明;"`                                                               //备注说明
	CreatedBy       uint     `gorm:"column:created_by;comment:创建者"`
	UpdatedBy       uint     `gorm:"column:updated_by;comment:更新者"`
	DeletedBy       uint     `gorm:"column:deleted_by;comment:删除者"`
}

// GetReservationInfoList 分页获取reservation表记录
// Author [yourname](https://github.com/yourname)
func (reservationService *ReservationService) GetReservationInfoList(info reservationsReq.ReservationSearch) (list []ResponseReservation, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&reservations.Reservation{})
	var reservationss []reservations.Reservation
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	var sli []ResponseReservation
	err = db.Find(&reservationss).Error
	for _, r := range reservationss {
		var one patients.Patient
		global.GVA_DB.Where("id=?", r.PatientId).Find(&one)
		two := ResponseReservation{
			GVA_MODEL: global.GVA_MODEL{
				ID:        r.ID,
				CreatedAt: r.CreatedAt,
				UpdatedAt: r.UpdatedAt,
				DeletedAt: r.DeletedAt,
			},
			ReservationSn:   r.ReservationSn,
			PatientId:       r.PatientId,
			PatientCardNum:  one.PatientCardNum,
			Name:            one.Name,
			Sex:             one.Sex,
			Mobile:          one.Mobile,
			ReservationObj:  r.ReservationObj,
			ReservationType: r.ReservationType,
			Department:      r.Department,
			PayType:         r.PayType,
			Price:           r.Price,
			Menu:            r.Menu,
			CreatedBy:       r.CreatedBy,
			UpdatedBy:       r.UpdatedBy,
			DeletedBy:       r.DeletedBy,
		}
		sli = append(sli, two)
	}
	return sli, total, err
}
func (reservationService *ReservationService) GetReservationPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
